package com.mrjy.coursepackage.bo;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mrjy.common.entity.CourseInfo;
import com.mrjy.common.entity.CoursePackageVideoRel;
import com.mrjy.common.entity.CoursePackageVip;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.coursepackage.dao.ICoursePackageVideoRelDao;
import com.mrjy.coursepackage.dao.ICoursePackageVipDao;
import com.mrjy.coursepackage.dto.CoursePackageVideoRelDTO;

/**
 * 课程包和视频关联关系 业务实现类
 * @author csshui
 *
 */
@Service("coursePackageVideoRelBo")
public class CoursePackageVideoRelBo implements ICoursePackageVideoRelBo {

	@Resource
	private ICoursePackageVideoRelDao coursePackageVideoRelDao;
	
	@Resource
	private ICoursePackageVipDao coursePackageVipDao;
	
	@Override
	public Object txInsert(CoursePackageVideoRel info) {
		Map<String, Object> map = new HashMap<String, Object>();
		int count = coursePackageVideoRelDao.insert(info);
		if (count > 0 ) {
			map.put("err_code", 0);
			map.put("err_msg", "插入成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "插入失败");
		}
		return map;
	}

	@Override
	public Object txUpdate(CoursePackageVideoRel info) {
		Map<String, Object> map = new HashMap<String, Object>();
		int count =  coursePackageVideoRelDao.update(info);
		if (count > 0 ) {
			map.put("err_code", 0);
			map.put("err_msg", "更新成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "更新失败");
		}
		return map;
	}

	@Override
	public Object txUpdateById(CoursePackageVideoRel info) {
		Map<String, Object> map = new HashMap<String, Object>();
		int count =  coursePackageVideoRelDao.updateById(info);
		if (count > 0 ) {
			map.put("err_code", 0);
			map.put("err_msg", "更新成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "更新失败");
		}
		return map;
	}

	@Override
	public Object txDelete(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		int count = coursePackageVideoRelDao.delete(id);
		if (count > 0 ) {
			map.put("err_code", 0);
			map.put("err_msg", "删除成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "删除失败");
		}
		return map;
	}

	@Override
	public Object selectById(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		CoursePackageVideoRelDTO info = coursePackageVideoRelDao.selectById(id);
		if ( null != info ) {
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("info", info);
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "查询失败");
			map.put("info", info);
		}
		return map;
	}

	@Override
	public Object txInsertVideos(CoursePackageVideoRelDTO dto) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(dto == null){
			map.put("err_code", -1);
			map.put("err_msg", "新增的信息为空");
			return map;
		}
		List<Integer> list = dto.getVideoIds();
		int count = list.size();
		List<Integer> existList = coursePackageVideoRelDao.selectVideoIdListByCoursePackageId(dto.getCoursePackageId());
		list.removeAll(existList);
		if (list != null && list.size() > 0) {
			for(Integer videoId : list){
				CoursePackageVideoRel rel = new CoursePackageVideoRel();
				rel.setCoursePackageId(dto.getCoursePackageId());
				rel.setVideoId(videoId);
				rel.setCreateTime(new Timestamp(System.currentTimeMillis()));
				rel.setSort(100);//排序默认 100
				coursePackageVideoRelDao.insert(rel);
			}
			map.put("err_code", 0);
			map.put("err_msg", "插入成功");
			map.put("total", count);
		}else if(count > 0){
			map.put("err_code", 1);
			map.put("err_msg", "插入的数据已在数据库中，无需操作");
		}else {
			map.put("err_code", 2);
			map.put("err_msg", "插入失败");
		}
		
		return map;
	}

	@Override
	public Object selectPage(CoursePackageVideoRelDTO dto) {
		Map<String, Object> map = new HashMap<String, Object>();
		int count = coursePackageVideoRelDao.selectCountPage(dto);
		List<CoursePackageVideoRelDTO> list = new ArrayList<CoursePackageVideoRelDTO>();
		if(count > 0){
			list = coursePackageVideoRelDao.selectPage(dto);
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("total", count);
			map.put("rows", list);
		}else{
			map.put("err_code", 1);
			map.put("err_msg", "查询失败");
			map.put("total", count);
			map.put("rows", list);
		}
		return map;
	}

	@Override
	public Object txDeleteByIds(int[] ids) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(ids.length == 0){
			map.put("err_code", -1);
			map.put("err_msg", "id为空");
			return map;
		}
		int count =0 ;
		for(int id : ids){
			count += coursePackageVideoRelDao.delete(id);
		}
		if(count > 0){
			map.put("err_code", 0);
			map.put("err_msg", "删除成功");
			map.put("total", count);
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "删除失败");
			
		}
		return map;
	}

	@Override
	public Object selectByCoursePackageIdAndVideoId(CoursePackageVideoRelDTO paramDto) {
		Map<String, Object> map = new HashMap<String, Object>();
		CoursePackageVideoRelDTO dto = coursePackageVideoRelDao.selectByCoursePackageIdAndVideoId(paramDto);
		if(dto != null){
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("info", dto);
		}else{
			map.put("err_code", 1);
			map.put("err_msg", "查询失败");
		}
		return map;
	}

	@Override
	public Object selectCoursePackageByVideoId(CoursePackageVideoRelDTO paramDto , UserInfo info) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<CoursePackageVideoRelDTO> list = coursePackageVideoRelDao.selectCoursePackageByVideoId(paramDto);
        int total = coursePackageVideoRelDao.selectCountCoursePackageByVideoId(paramDto);
        if(list != null && list.size() > 0){
			//根据用户id查询所有已购买的课程包
			List<CoursePackageVip> vipList = coursePackageVipDao.selectVipByUserId(info == null?null:info.getId());
			for(CoursePackageVideoRelDTO dto : list){
				if(null != dto && dto.getCoursePackageId() != null){
                    List<CoursePackageVideoRelDTO> coursePackageVideoRelList = coursePackageVideoRelDao.selectByCoursePackageId(info == null?null:info.getId(),dto.getCoursePackageId());
                    dto.setCoursePackageVideoRelList(coursePackageVideoRelList);
					dto.setVideoNum(coursePackageVideoRelList.size());
					if(vipList !=null && vipList.size() > 0){
						for(CoursePackageVip vip : vipList){
							if(dto.getCoursePackageId() == vip.getCoursePackageId()){
								dto.setIsVipStatus(1);
								break;
							}else{
								dto.setIsVipStatus(0);
							}
						}
					}else{
						dto.setIsVipStatus(0);
					}
					
				}
            }

            map.put("total_course", total);
            map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("rows", list);
		}else{
			map.put("err_code", 1);
			map.put("err_msg", "查询失败");
			map.put("rows", list);
		}
		return map;
	}

}
